<?php
class helper_date extends system_view_helper_abstract {
	
	private $_date = null;
	/**
	 * @return helper_date
	 */
	public function date($date = null, $format = null) {
		$this->_defaultFormat = $format;
		if($date === null) {
			$this->_date = new system_date();
		} else {
			$this->_date = new system_date($date);
		}
		return $this;
	}
	
	protected $_timezone = array();
	public function assemble($lang = null) {
		if($lang === null) {
			$timezone = $this->getDefaultTimezone();
		} else {
			if(!isset($this->_timezone[$lang])) {
				$concept = new system_setting_language($lang);
				$timezone = $this->_timezone[$lang] = $concept->getTimezone();
			} else {
				$timezone = $this->_timezone[$lang];
			}
		}
		return $this->_date->setTimezone($timezone)->toString($this->getDefaultFormat());
	}
	
	public function __toString() {
		return $this->assemble();
	}
	
	protected $_defaultTimezone = null;
	protected function getDefaultTimezone() {
		if($this->_defaultTimezone === null) {
			$nav = system_nav_language::getInstance();
			if(!($this->_defaultTimezone = system_nav_language::getInstance()->getDefaultTimezone())) {
				$this->_defaultTimezone = 'PST';
			}
		}
		return $this->_defaultTimezone;
	}
	
	protected $_defaultFormat = null;
	protected function getDefaultFormat() {
		if($this->_defaultFormat === null) {
			if(preg_match('/^zh_/i', system_nav_language::getInstance()->getName())) {
				$this->_defaultFormat = 'Y-m-d H:i:s';
			} else {
				$this->_defaultFormat = 'M d, Y, H:i:s';
			}
		}
		return $this->_defaultFormat;
	}

}